<?php


namespace app\applet\controller;


use app\utils\JwtUtils;
use think\Controller;
use think\Db;

class Login extends Controller
{
    //业主绑定房屋
    public function bindRoom()
    {
        $accessToken=request()->header("accessToken");
        if(empty($accessToken)){
            return json(["code"=>10020,'msg' => '用户未登录']);
        }
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $client=request()->header("clientType");

        $userInfo=Db::table("userinfo_wechat")
            ->where("openid",$userResult["openid"])
            ->find();

        if(!empty($userInfo)){
            return json(["code"=>1,'msg' => '您已经绑定房屋，无需重复绑定']);
        }
        $data=request()->post();
        //手机号验证
        $smsverify=Db::table("sms_verify")
            ->where("phone",$data["phone"])
            ->where("code",$data["verificationCode"])
            ->where("ctime",'>',(time()-80))
            ->order("id desc")
            ->find();
        if(empty($smsverify))
        {
            return json(["code"=>1,'msg' => '验证码错误']);
        }else
        {
            $info["status"]="2";
            Db::table("sms_verify")
                ->where("id",$smsverify["id"])
                ->update($info);
            //执行绑定房屋操作
            $roominfo=Db::table("roominfo")
                ->where("phone",$data["phone"])
                ->find();
            if(empty($roominfo))
            {
                return json(["code"=>0,'msg' => '房屋绑定失败，请重新获取验证码绑定1']);
            }

            $add_info["openid"]=$userResult["openid"];
            //查询该房屋是否已经绑定
            $user=Db::table("userinfo_wechat")
                ->where("room_id",$roominfo["id"])
                ->find();
            if(empty($user)){
                $add_info["phone"]=$data["phone"];
                $add_info["room_id"]=$roominfo["id"];
                $add_info["insert_time"]=date('Y-m-d H:i:s');
                $add_info["update_time"]=date('Y-m-d H:i:s');
                //Log::write($data,"yang");
                $res=Db::table("userinfo_wechat")->insertGetId($add_info);
                if($res)
                {
                    $add_info["uid"]=$res;
                    $add_info["bindRoomStatus"]=true;
                    $token=JwtUtils::getToken($add_info);
                    return json(["code"=>0,'msg' => '房屋绑定成功',"accessToken"=>$token]);
                }else
                {
                    return json(["code"=>1,'error' => '房屋绑定失败，请重新获取验证码绑定3']);
                }
            }else{


                $add_info["phone"]=$data["phone"];
                $add_info["update_time"]=date('Y-m-d H:i:s');
                //Log::write($data,"yang");
                $res=Db::table("userinfo")->where("id",$user["id"])->update(
                    $add_info
                );
                if($res)
                {
                    $add_info["room_id"]=$roominfo["id"];
                    $add_info["uid"]=$user["id"];
                    $add_info["bindRoomStatus"]=true;
                    $token=JwtUtils::getToken($add_info);
                    return json(["code"=>0,'msg' => '房屋绑定成功',"accessToken"=>$token]);
                }else
                {
                    return json(["code"=>1,'error' => '房屋绑定失败，请重新获取验证码绑定3']);
                }
            }

        }
    }


    //业主获取短信验证码
    public function getSmsCode()
    {

        //return json(["code"=>0,'msg' => '验证码发送成功']);
        $data=request()->post();
        //查询该用户是否绑定房屋

        //查询该手机号下是否存在房屋
        $roominfo=Db::table("roominfo")->where("phone",$data["phone"])->find();
        if(empty($roominfo))
        {
            return json(["code"=>500,'msg' => '亲爱的业主：您的手机号码验证错误，可能是预留在物业服务中心手机号码没有及时更新，为更快的解决您的问题，您可以在线客服留言或致电我们，感谢您的理解支持。']);
        }
        //查询该房屋是否已经绑定用户
        $userinfo=Db::table("userinfo")
            ->where("room_id",$roominfo["id"])
            ->find();
        $client=request()->header("clientType");

        if(!empty($userinfo))
        {
            if(($client=="WECHAT_MP") && !empty($userinfo["openid"])){
                return json(["code"=>500,'msg' => '该房屋已经绑定']);
            }
            if(($client=="ALIPAY") && !empty($userinfo["userid"])){
                return json(["code"=>500,'msg' => '该房屋已经绑定']);
            }

        }
        //return json(["code"=>0,"msg"=>"123"]);

        $result=$this->sendmsg($data["phone"]);
        return $result;
    }

    //短信验证码
    public function sendmsg($phone)
    {
        $code=rand(100000,999999);
        //$phone=request()->param("phone");
        $send = send_sms($phone, 1, ['code' => $code]);
        if($send['Message'] == 'OK')
        {
            $info["phone"]=$phone;
            $info["code"]=$code;
            $info["ctime"]=time();
            $result=Db::table("sms_verify")->insert($info);
            if($result)
            {
                return json(["code"=>0,'msg' => '验证码发送成功']);

                //die;
            }else
            {
                return json(["code"=>500,'msg' => '请重新获取验证码']);
            }
        } else
        {
            return json(["code"=>500,'msg' => $send['Message']]);

        }
    }
}